#include <ESP8266WiFi.h>

#define RELAY 0    //connected to GPIO0

const char *ssid = "YXS1302";
const char *password = "YuanXueShe1302";

WiFiServer server(80);

void setup()
{
    Serial.begin(115200);    // Must have the same baud rate as the serial monitor.
    pinMode(RELAY, OUTPUT);
    digitalWrite(RELAY, HIGH);

    /* Connect to the WiFi network. */
    Serial.println("");
    Serial.print("Conneting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");

    /* Start the Server. */
    server.begin();
    Serial.println("Server started.");

    /* Output of the IP address. */
    Serial.print("Use this URL to connect: ");
    Serial.print("http://");
    Serial.print(WiFi.localIP());
    Serial.println("/");
}

void loop()
{
    /* Check whether a user has established a connection. */
    WiFiClient client = server.available();
    if (!client)
        return;

    /* Wait until the user sends data. */
    Serial.println("new client.");
    while (!client.available())
        delay(1);

    /* Reading the first line of the request. */
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    /* Assignment of the request. */
    int value = HIGH;
    if (request.indexOf("/RELAY=ON") != -1) {
        Serial.println("RELAY=ON");
        digitalWrite(RELAY, LOW);
        value = LOW;
    }
    if (request.indexOf("/RELAY=OFF") != -1) {
        Serial.println("RELAY=OFF");
        digitalWrite(RELAY, HIGH);
        value = HIGH;
    }

    /* Return the answer. */
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("");
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    client.println("<meta name='viewport' content='width=device-width, inital-scale=1'>");
    client.println("<head><title>ESP8266 RELAY Control</title></head>");
    client.println("<style>");
    client.println("* {text-align: center; font-family: Conso-las;}");
    client.println("");
    client.println(".container {position: absolute; top: 45%;");
    client.println("left: 50%; transform: translateX(-50%) translateY(-50%);}");
    client.println("");
    client.println(".inner {font-size: 40px;}");
    client.println("");
    client.println("a {display: inline-block; width: 300px; height: 100px;");
    client.println("color: #FFFF00; background-color: #275C6B;");
    client.println("margin-top: 20px; padding: 10px 20px;");
    client.println("text-decoration: none; border-radius: 5px;");
    client.println("font-weight: 800; font-size: 70px;}");
    client.println("</style>");
    client.println("<div class='container'>");
    client.println("<div class='inner'>");
    client.print("RELAY if now: ");

    if (value == HIGH)
        client.print("OFF");
    else
        client.print("ON");

    client.println("<br>");
    client.println("<a href=\"/RELAY=OFF\">OFF</a>");
    client.println("<a href=\"/RELAY=ON\">ON</a>");
    client.println("<style>");
    client.println(".footer-text { font-size: 25px; }");
    client.println(".bold-text { font-weight: bold; }"); 
    client.println("</style>");
    client.println("<div class='footer'>");
    client.println("<p class='footer-text'>This software was written by <span class='bold-text'>Grayson Zheng</span>.</p>");
    client.println("</div>");
    client.println("</div>");
    client.println("</html>");
    client.println("");
}
